package com.xci.sys.api;

import com.xci.core.base.RestMessage;
import com.xci.core.internal.Const;
import com.xci.core.helper.ExcelHelper;
import com.xci.core.annotation.Authorize;
import com.xci.core.base.ApiController;
import com.xci.sys.entity.SysChart;
import com.xci.sys.service.ChartService;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 图表接口
 * @author 吕艳阳
 */
@ApiSort(15)
@Api(tags = "图表接口")
@RestController
@Authorize
@RequestMapping(value = "/api/sys/chart", produces = Const.P_JSON)
public class ChartApiController extends ApiController {
    @Resource
    private ChartService chartService;//图表服务

    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "新建图表")
    @PostMapping("/insert")
    public RestMessage insert(@RequestBody SysChart entity) {
        return chartService.insert(entity);
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "修改图表")
    @PostMapping("/update")
    public RestMessage update(@RequestBody SysChart entity) {
        return chartService.update(entity);
    }

    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "删除图表")
    @ApiImplicitParam(name = "codes", value = "图表编码字符串", required = true)
    @PostMapping("/delete")
    public RestMessage delete(String codes) {
        return chartService.delete(codes);
    }

    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "检查图表编码是否存在")
    @ApiImplicitParam(name = "code", value = "图表编码", required = true)
    @PostMapping("/existByCode/{code}")
    public RestMessage<Boolean> existByCode(@PathVariable String code) {
        return RestMessage.result(chartService.existByCode(code));
    }

    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "根据编码查询单个图表")
    @ApiImplicitParam(name = "code", value = "图表编码", required = true)
    @PostMapping("/selectByCode/{code}")
    public RestMessage<SysChart> selectByCode(@PathVariable String code) {
        return RestMessage.success(chartService.selectByCode(code));
    }

    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "查询图表列表")
    @PostMapping("/selectList")
    public RestMessage<List<SysChart>> selectList() {
        return RestMessage.success(chartService.selectList());
    }

    @ApiOperationSupport(order = 7)
    @ApiOperation(value = "导出图表列表")
    @PostMapping(value = "/export", produces = {Const.P_OCTET, Const.P_JSON})
    public void export() {
        ExcelHelper.exportWeb(chartService.selectList(), SysChart.class, "系统图表列表");
    }

    @ApiOperationSupport(order = 8)
    @ApiOperation(value = "刷新图表缓存")
    @PostMapping(value = "/refreshCache")
    public RestMessage refreshCache() {
        chartService.refreshCache();
        return RestMessage.success();
    }
}